//
//  HeaderReusableView.m
//  PetAPP
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "HeaderReusableView.h"
#import "NetworkingHandle.h"
#import "ModelOfHeaderImage.h"
#import "ModelOfHeaderInfo.h"
#import "LVView.h"
#import <YYWebImage.h>

@interface HeaderReusableView () <UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;

@property (nonatomic, retain) NSMutableArray *headerImageArr;

@property (nonatomic, retain) NSMutableArray *headerInfoArr;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, assign) BOOL timeFlag;

@property (nonatomic, retain) LVView *viewOfBeLost;

@property (nonatomic, retain) LVView *viewOfActivity;

@property (nonatomic, retain) LVView *viewOfFind;

@end

@implementation HeaderReusableView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        CGRect newFrame = (CGRect){.size = self.frame.size};
        [self createSubViewWithFrame:newFrame];
        [self handleData];
        [self createImgInfoViewWithFrame:newFrame];
    }
    return self;
}
#pragma mark - 创建scroll
- (void)createSubViewWithFrame:(CGRect)frame {
    
    CGFloat width = CGRectGetWidth(frame);
    /** 设置scroll的大小 */
    self.scroll = [[UIScrollView alloc] initWithFrame:(CGRect){.size = CGSizeMake(width, 145)}];
    [self addSubview:self.scroll];
    [_scroll release];
    self.scroll.backgroundColor = [UIColor whiteColor];
    /** 指定scroll的代理 */
    self.scroll.delegate = self;
    /** 开启翻页效果. */
    self.scroll.pagingEnabled = YES;
    /** 到边缘是否有回弹效果 */
    self.scroll.bounces = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"timeStop" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        [self.timer invalidate];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"timeStart" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHandleAction) userInfo:nil repeats:YES];
    }];
    
}
#pragma mark - 加载并处理数据
- (void)handleData {
    NSString *url = @"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=indexbanner";
    NSString *body = @"version=d1bd83a33f1a841ab7fda32449746cc4";
    NSDictionary *dic = @{@"Content-Type" : @"application/x-www-form-urlencoded"};
    /** 接收数据的数组 */
    self.headerImageArr = [NSMutableArray new];
    self.headerInfoArr = [NSMutableArray new];
    /** 接收数据处理放回值的block方法 */
    [NetworkingHandle handleWorkData:url AndBody:body value:dic handleBlock:^(id result) {
        
        /** 找到装轮播图的数组 */
        NSArray *tempArr = [result objectForKey:@"array"];
        /** 将数组中的内容遍历添加到model中 */
        for (NSDictionary *dic in tempArr) {
            ModelOfHeaderImage *model = [[ModelOfHeaderImage alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headerImageArr addObject:model];
            [model release];
        }
        /** 加载三张图的内容数据 */
        NSArray *tempArr2 = [result objectForKey:@"img_new_info"];
        for (NSDictionary *dic in tempArr2) {
            ModelOfHeaderInfo *model = [[ModelOfHeaderInfo alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headerInfoArr addObject:model];
            [model release];
        }
        
        /** model添加完毕以后根据model的数量创建轮播图 */
        
        /** 根据model的个数创建scroll的大小 比model多两个 在头部和尾部多添加2张图片 */
        self.scroll.contentSize = CGSizeMake(self.frame.size.width * (self.headerImageArr.count + 2), 145);
        /** 调用创建图片的方法 */
        [self createScrollWithFrame:self.scroll.frame];
        
        
        /** 调用加载图片方法 */
        [self changeLVContent];
       
 
    }];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeHandleAction) userInfo:nil repeats:YES];
    
}
#pragma mark - 给下面三张图片赋值
- (void)changeLVContent {
    for (int i = 0; i < self.headerInfoArr.count; i++) {
        switch (i) {
            case 0:{
                [self.viewOfBeLost handleDataWithModel:self.headerInfoArr[i]];
                break;
            }
            case 1:{
                [self.viewOfActivity handleDataWithModel:self.headerInfoArr[i]];
                break;
            }
            case 2:{
                [self.viewOfFind handleDataWithModel:self.headerInfoArr[i]];
                break;
            }
        }
        
    }

}

#pragma mark - 创建图片的方法
- (void)createScrollWithFrame:(CGRect)frame {
    for (int i = 0 ; i < self.headerImageArr.count + 2; i++) {
        ModelOfHeaderImage *model = nil;
        if (i == 0) {
            model = self.headerImageArr[self.headerImageArr.count - 1];
        }else if (i == self.headerImageArr.count + 1){
            model = self.headerImageArr[0];
        }else{
            model = self.headerImageArr[i - 1];
        }
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.yy_imageURL =  [NSURL URLWithString:model.coverpath];
        imageView.frame = CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height);
        [self.scroll addSubview:imageView];
        [imageView release];
    }
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
}
#pragma mark - 轮播图自动播放控制
- (void)timeHandleAction {

    [self.scroll setContentOffset:CGPointMake(self.scroll.contentOffset.x + self.frame.size.width, 0) animated:YES];
    CGFloat width = self.frame.size.width;
    if ((width * (self.headerImageArr.count + 1)) == self.scroll.contentOffset.x){
        self.scroll.contentOffset = CGPointMake(width , 0);
    }
}

#pragma mark - scroll的代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pointX = scrollView.contentOffset.x;
    CGFloat width = self.frame.size.width;
    if (0 == pointX) {
        float sizeX = (self.headerImageArr.count) * width;
        scrollView.contentOffset = CGPointMake(sizeX , 0);
    }
    if ((width * (self.headerImageArr.count + 1)) == pointX){
        scrollView.contentOffset = CGPointMake(width , 0);
    }
//    self.timeFlag = NO;
    
    
}

#pragma mark - 推荐信息层
- (void)createImgInfoViewWithFrame:(CGRect)frame {

    
    /** 创建新下面图层的大小 */
    CGRect newFrame = CGRectMake(0, 145, frame.size.width, 105);
    UIView *viewOfInfo = [[UIView alloc] initWithFrame:newFrame];
    viewOfInfo.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 104, frame.size.width, 1)];
    lineView.layer.borderWidth = 1;
    lineView.layer.borderColor = [UIColor grayColor].CGColor;
    [viewOfInfo addSubview:lineView];
    
    
    self.viewOfBeLost = [[LVView alloc] initWithFrame:CGRectMake(40, 10, (newFrame.size.width - 40 * 4) / 3, newFrame.size.height - 20)];
    [viewOfInfo addSubview:self.viewOfBeLost];
    [_viewOfBeLost release];
    self.viewOfBeLost.tag = 1;
    
    
    self.viewOfActivity = [[LVView alloc] initWithFrame:CGRectMake((newFrame.size.width - 40 * 4) / 3 + 40 * 2, 10, (newFrame.size.width - 40 * 4) / 3, newFrame.size.height - 20)];
    [viewOfInfo addSubview:self.viewOfActivity];
    [_viewOfActivity release];
    self.viewOfActivity.tag = 2;
    
    self.viewOfFind = [[LVView alloc] initWithFrame:CGRectMake((newFrame.size.width - 40 * 4) / 3 * 2 + 40 * 3, 10, (newFrame.size.width - 40 * 4) / 3, newFrame.size.height - 20)];
    [viewOfInfo addSubview:self.viewOfFind];
    [_viewOfFind release];
    self.viewOfFind.tag = 3;
    
    
    [self addSubview:viewOfInfo];
    [viewOfInfo release];


}






#pragma mark - collectionCell 的子控件的布局通常在此方法中设置
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
 
    
}

- (void)dealloc {
    [_viewOfActivity release];
    [_viewOfBeLost release];
    [_viewOfFind release];
    [_timer release];
    [_scroll release];
    [_headerImageArr release];
    [_headerInfoArr release];
    [super dealloc];
}

@end
